package com.java.control.impl;


import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.springframework.validation.BindException;

import com.java.common.SessionInfo;
import com.java.control.AbstractAdminFormController;
import com.java.entity.Product;
import com.java.entity.Supplier;
import com.java.entity.SupplierQuotation;
import com.java.entity.User;
import com.java.service.ProductService;
import com.java.service.SupplierQuotationService;
import com.java.service.SupplierService;

/**
 * 产品报价添加
 * @author wqf
 * @version  Nov 23, 2011 1:10:42 PM
 */
public class SupplierQuotationEditController extends AbstractAdminFormController{

	public SupplierQuotationEditController(){
		this.setCommandClass(SupplierQuotation.class);
	}
		
	protected void addSubmit(HttpServletRequest request,HttpServletResponse response, Object command, BindException errors) throws Exception {
		SupplierQuotation product = (SupplierQuotation)command;
		SessionInfo sessionInfo = this.getSessionInfo(request);
		if(null!=sessionInfo&&(product.getUser()==null||product.getUser().getId()==null)){
			User user = new User();
			user.setId(sessionInfo.getUserId());
			product.setUser(user);
		}
		this.supplierQuotationService.editSupplierQuotation(product);
		request.setAttribute("operation", "add");		
		this.pt(response, "<script>window.parent.afterSubmit()</script>");
	}	
	
	protected void modifySubmit(HttpServletRequest request,HttpServletResponse response,Object command,BindException errors) throws Exception{		
		SupplierQuotation product = (SupplierQuotation)command;
		SessionInfo sessionInfo = this.getSessionInfo(request);
		if(null!=sessionInfo&&(product.getUser()==null||product.getUser().getId()==null)){
			User user = new User();
			user.setId(sessionInfo.getUserId());
			product.setUser(user);
		}
		String tag = request.getParameter("copy");
		if(StringUtils.isNotBlank(tag)&&tag.equals("1")){
			product.setId(null);//复制
		}
		this.supplierQuotationService.editSupplierQuotation(product);
		
		request.setAttribute("operation", "modify");
		this.pt(response, "<script>window.parent.afterSubmit()</script>");
	}
		
	protected Object getCommandDataForModify(HttpServletRequest request){
		String id = request.getParameter("id");
		if(null!=id){
			SupplierQuotation quotation = this.supplierQuotationService.getSupplierQuotationById(Long.parseLong(id));
			if(quotation.getProduct()==null){
				quotation.setProduct(new Product());				
			}
			if(quotation.getSupplier()==null){
				quotation.setSupplier(new Supplier());
			}			
			String tag = request.getParameter("copy");
			if(StringUtils.isNotBlank(tag)&&tag.equals("1")){
				quotation.setSupplier(new Supplier());//复制
			}
			return quotation;
		}
		return null;
	}
	
	protected Object getCommandDataForAdd(HttpServletRequest request) {
		SupplierQuotation sq = new SupplierQuotation();
		String pid = request.getParameter("pid");
		String sid = request.getParameter("sid");
		if(null!=pid){
			Product product = this.productService.getProductById(Long.parseLong(pid));
			sq.setProduct(product);			
		}else{
			sq.setProduct(new Product());
		}
		if(null!=sid){
			Supplier supplier = this.supplierService.getSupplierById(Long.parseLong(sid));
			sq.setSupplier(supplier);			
		}else{
			sq.setSupplier(new Supplier());
		}
		return sq;
	}
	
	public void pt(HttpServletResponse response,String text){
		response.setContentType("text/html;charset=UTF-8");
		PrintWriter pw;
		try {
			pw = response.getWriter();
			pw.write(text);
			pw.flush();
			pw.close();
		} catch (IOException e) {
			e.printStackTrace();
		}		
	}
	
	public SupplierQuotationService supplierQuotationService;
	public ProductService productService;
	public SupplierService supplierService;
	
	public SupplierQuotationService getSupplierQuotationService() {
		return supplierQuotationService;
	}

	public void setSupplierQuotationService(
			SupplierQuotationService supplierQuotationService) {
		this.supplierQuotationService = supplierQuotationService;
	}

	public ProductService getProductService() {
		return productService;
	}

	public void setProductService(ProductService productService) {
		this.productService = productService;
	}

	public SupplierService getSupplierService() {
		return supplierService;
	}

	public void setSupplierService(SupplierService supplierService) {
		this.supplierService = supplierService;
	}

	
}
